<?php

class SignupForm extends Form
{
	public $MailModule = 'System/Data/Auth';
	public $MailTemplate = 'templates/newuser.tpl';
	public $MailSubject = 'Welcome';
	
	public function SignupForm()
	{
		parent::__construct();
		
		$this->Name = 'signup';
		$this->Title = translate('Signup');
		$this->ButtonText = translate('Signup');
		
		// add controls to section
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'user', 'Title' => translate('Loginname'), 'IsBound' => false, 
			'Validators' => array( new StringLengthValidator(6), new RegExValidator('[^A-Za-z0-9]', translate('Can consist of letters and numbers only.'))) ));
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'fullname', 'Title' => translate('Full name'), 'IsBound' => false ) );
		$this->Controls[] = a2o( 'FormControl', array( 'FieldName' => 'email', 'Title' => translate('Email'), 'IsBound' => false, 'Validator' => new EmailAddressValidator() ) );
		$this->pass = a2o( 'FormControl', array( 'FieldName' => 'password', 'Title' => translate('Password'), 'IsBound' => false, 'Type' => 'password', 'Validator' => new StringLengthValidator(8) ) );
		$this->Controls[] = $this->pass;
		$this->pass2 = a2o( 'FormControl', array( 'FieldName' => 'password_re', 'Title' => translate('Password(re)'), 'IsBound' => false, 'Type' => 'password' ) );
		$this->Controls[] = $this->pass2;		
	}
	
	public function FormAction()
	{
		if(session('user') != '')
		{
			$this->Messages[] = translate('Logged in as') . ' ' . session('user') . '.';
			$this->HideControls = true;
		}
		else
		{
			// form load success?
			if( $this->LoadPostData())
			{
				$this->Signup( );
			}
			else
			{
				if($this->pass->GetValue() == '')
					$this->pass2->SetValue('');
			}
		}
	}

	public function Signup()
	{
		$LoginName = $_POST['user'];
		$FullName = $_POST['fullname'];
		$Email = $_POST['email'];
		$Password = $_POST['password'];
		$PasswordRe = $_POST['password_re'];
		
		if($Password == $PasswordRe)
		{
			$Result = UserManager::Signup( $LoginName, $Password, $FullName, $Email );
			
			if( $Result > 0 )
			{
				session_start();
				// log the user in
				$_SESSION['user'] = $LoginName;
				App::$Current->User = $LoginName;
				session_write_close();
				
				// send email with loginname and password
				$ConfirmURL = ConfirmRequestManager::GenerateRequestURL('http://localhost/confirmemail', $LoginName, $Email );
				$to = $_POST['fullname'] . '<' . $_POST['email'] . '>';					
				$mailTemplate = a2o('TemplateLoader', array( 'Module' => $this->MailModule, 'Template' => $this->MailTemplate,
					'User' => $LoginName, 'Password' => $Password, 'FullName' => $FullName, 'ConfirmURL' => $ConfirmURL ) );
				Mail::Send($to, $this->MailSubject, $mailTemplate->GetText());
				//mail($to, $this->MailSubject, $mailTemplate->translate( ));
				//$this->Messages[] = $mailTemplate->translate();
			
				// success
				$this->HideControls = true;
				$this->Messages[] = translate('Successfully signed up.');
			}
			else
				$this->Messages[] = translate('Could not sign you up.');
		}
		else
			$this->Messages[] = translate('The passwords do not match.');
	}
}
